package com.example.student.service.impl;
import com.example.student.mapper.StuMapper;
import com.example.student.pojo.PageBean;
import com.example.student.pojo.Result;
import com.example.student.pojo.Stu;
import com.example.student.service.StuService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @author 34082
 */
@Service
@Slf4j
public class StuServiceImpl implements StuService {

    private StuMapper stuMapper;
    @Autowired
    public void setStuMapper(StuMapper stuMapper) {
        this.stuMapper = stuMapper;
    }

    @Override
    public List<Stu> list() {
        return stuMapper.list();
    }

    @Override
    public void delete(Integer id) {
        stuMapper.deleteById(id);
    }


    @Override
    public void update(Stu stu) {
        stu.setUpdateTime(LocalDateTime.now());
        stuMapper.update(stu);
    }

    @Override
    public Result add(Stu stu) {
        stu.setCreateTime(LocalDateTime.now());
        stu.setUpdateTime(LocalDateTime.now());
        int max=stuMapper.findMax();
        log.info("学生学号:{}", max+1);
        stu.setStudentId(max+1);
        stuMapper.insert(stu);
        return Result.success();
    }

    @Override
    public Stu getById(Integer id) {
        return stuMapper.getById(id);
    }

    @Override
    public PageBean page(Integer page, Integer pageSize) {
        System.out.println(page);
        System.out.println(pageSize);

        long count=stuMapper.count();
        Integer start =(page-1)*pageSize;
        List<Stu> list=stuMapper.page(start,pageSize);

        return new PageBean(count,list);
    }

    @Override
    public PageBean pages(Integer page, Integer pageSize, Integer studentId,String name,Short gender) {
        System.out.println(page);
        System.out.println(pageSize);

        long count = stuMapper.counts(studentId, name, gender);
        Integer start =(page-1)*pageSize;
        List<Stu> list=stuMapper.pages(start,pageSize,studentId,name,gender);

        return new PageBean(count,list);
    }

    @Override
    public PageBean classPage(Integer page, Integer pageSize, Integer deptId) {
        long count = stuMapper.classCounts(deptId);
        Integer start =(page-1)*pageSize;
        List<Stu> list=stuMapper.classPages(start,pageSize,deptId);

        return new PageBean(count,list);
    }
}
